package cn.wl.hrm.service;


import cn.wl.hrm.doc.CourseDoc;
import cn.wl.hrm.query.CourseQuery;
import cn.wl.hrm.repository.CourseElasticsearchRepository;
import cn.wl.hrm.util.PageList;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class ESService {
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;

    public PageList<CourseDoc> searchCourse(CourseQuery courseQuery) {
        //将courseQuery对象转换为NativeSearchQueryBuilder对象去查询
        //构建查询
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //组合查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //must--必须匹配的条件
        List<QueryBuilder> must = boolQueryBuilder.must();
        //match--相当于模糊查询
        String keyword = courseQuery.getKeyword();
        System.out.println(keyword);
        if(StringUtils.hasLength(courseQuery.getKeyword())){
            must.add(QueryBuilders.matchQuery("name", courseQuery.getKeyword()));
        }
        //过滤条件
        List<QueryBuilder> filter = boolQueryBuilder.filter();
        //term--相当于等于或不等于
        if(courseQuery.getProductType() != null){
            filter.add(QueryBuilders.termQuery("courseTypeId", courseQuery.getProductType()));
        }
        //range--范围查询
        if(courseQuery.getPriceMin() != null){
            filter.add(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMin()));
        }
        if(courseQuery.getPriceMax() != null){
            filter.add(QueryBuilders.rangeQuery("price").lte(courseQuery.getPriceMax()));
        }
        //将组合查询条件放进nativeSearchQueryBuilder中
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        //根据什么排序
        if(StringUtils.hasLength(courseQuery.getSortField())){
            //默认排序字段
            String sortFieldName = "price";
            switch (courseQuery.getSortField().toUpperCase()){
                case "XL": sortFieldName = "saleCount";break;
                case "XP": sortFieldName = "onlineTime";break;
                case "PL": sortFieldName = "commentCount";break;
                case "JG": sortFieldName = "price";break;
                case "RQ": sortFieldName = "viewCount";break;
            }
            //排序方式
            //默认排序--倒序
            SortOrder sortOrder = SortOrder.DESC;
            if(StringUtils.hasLength(courseQuery.getSortType()) && courseQuery.getSortType().toUpperCase().equals("ASC")){
                sortOrder = SortOrder.ASC;
            }
            nativeSearchQueryBuilder.withSort(new FieldSortBuilder(sortFieldName).order(sortOrder));
        }
        //分页条件
        nativeSearchQueryBuilder.withPageable(PageRequest.of(courseQuery.getPage()-1,courseQuery.getRows()));
        //执行查询，获取结果
        SearchQuery searchQuery = nativeSearchQueryBuilder.build();
        Page<CourseDoc> page = courseElasticsearchRepository.search(searchQuery);
        //将page集合转换为PageList集合，并返回
        return new PageList<CourseDoc>(page.getTotalElements(),page.getContent());
    }
}
